/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.recharge.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.github.wxpay.sdk.WXPayUtil;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.IdGen;
import com.jeeplus.common.utils.SnowFlake;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.WechatUtil;
import com.jeeplus.modules.account.entity.Account;
import com.jeeplus.modules.account.mapper.AccountMapper;
import com.jeeplus.modules.account.service.AccountService;
import com.jeeplus.modules.achieve.entity.Achievement;
import com.jeeplus.modules.achieve.service.AchievementService;
import com.jeeplus.modules.member.entity.Member;
import com.jeeplus.modules.member.mapper.MemberMapper;
import com.jeeplus.modules.member.service.MemberService;
import com.jeeplus.modules.plaformbank.entity.PlatformBank;
import com.jeeplus.modules.plaformbank.mapper.PlatformBankMapper;
import com.jeeplus.modules.positionassets.entity.PositionAssets;
import com.jeeplus.modules.wxgroup.entity.WxGroup;
import com.jeeplus.modules.wxgroup.service.WxGroupService;
import jdk.nashorn.internal.runtime.Undefined;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.recharge.entity.Recharge;
import com.jeeplus.modules.recharge.mapper.RechargeMapper;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 用户充值记录Service
 * @author w
 * @version 2020-09-28
 */
@Service
@Transactional(readOnly = true)
public class RechargeService extends CrudService<RechargeMapper, Recharge> {
	@Autowired
	private MemberMapper memberMapper;
	@Autowired
	private PlatformBankMapper platformBankMapper;
	@Autowired
	private WxGroupService wxGroupService;
	@Autowired
	private MemberService memberService;
	@Autowired
	private AccountService accountService;
	@Autowired
	private AccountMapper accountMapper;
	@Autowired
	private AchievementService achievementService;


	private final static String APPID = "wxf8333f0015bfad41";
	private final static String APPSECRET = "8104ad1318a445ace1bf2eb604330783";
	private final static String MCH_ID = "1607079032";
	private final static String PATERNERKEY = "yizhelegouyizhelegou111111686868";

	public Recharge get(String id) {
		return super.get(id);
	}
	
	public List<Recharge> findList(Recharge recharge) {
		return super.findList(recharge);
	}

	public List<Recharge> findListAll(Recharge recharge) {
		return mapper.findList(recharge);
	}
	
	public Page<Recharge> findPage(Page<Recharge> page, Recharge recharge) {
		String managerId = "";
		List<Recharge> accountList = new ArrayList<>();
		dataRuleFilter(recharge);
		recharge.setPage(page);
		if (StringUtils.isNotBlank(recharge.getMemberId())){
			Member member1 = memberMapper.getMemberByLoginName(recharge.getMemberId());
			if (member1 != null){
				recharge.setMemberId(member1.getId());
			}
		}
		if (recharge.getManager() != null){
			managerId = recharge.getManager().getId();
		}
		if (StringUtils.isNotBlank(managerId)){

			Member member1 = new Member();
			member1.setP(recharge.getManager());
//			member.setP(member.getManager());
			List<Member> memberList = memberService.findList(member1);
			//查询团队下级人员
			List<Member> queryList = new ArrayList<>();
			//最终集合
			List<Member> list = new ArrayList<>();
			queryList.addAll(memberList);
			list.addAll(memberList);
			while (list.size() > 0) {
				list.clear();
				List<Member> list1 = new ArrayList<>();
				for (Member member2 : queryList) {
					Member entity = new Member();
					entity.setP(member2);
//					member.setP(member2);
					List<Member> memberList1 = memberService.findList(entity);
					memberList.addAll(memberList1);
					list1.addAll(memberList1);
				}
				list.addAll(list1);
				queryList.clear();
				queryList.addAll(list1);
			}
			if (memberList.size() >0){
				for (Member member : memberList) {
					Recharge recharge1 = new Recharge();
					recharge1.setMemberId(member.getId());
					List<Recharge> list1 = findList(recharge1);
					accountList.addAll(list1);
				}
			}
			if (accountList.size()>0){
				for (Recharge recharge1 : accountList) {
					Member member = memberMapper.get(recharge1.getMemberId());
					recharge1.setMemberId(member.getLoginName());
				}
			}
			page.setCount(accountList.size());
			page.setList(accountList);
		}else {
			List<Recharge> list = mapper.findList(recharge);
			for (Recharge recharge1 : list) {
				Member member = memberMapper.get(recharge1.getMemberId());
				recharge1.setMemberId(member.getLoginName());
//			PlatformBank platformBank = platformBankMapper.get(recharge1.getRechargeType());
//			if (platformBank != null){
//				recharge1.setRechargeType(platformBank.getBankname());
//			}
			}
			page.setList(list);
		}

		return page;
//		return super.findPage(page, recharge);
	}
	
	@Transactional(readOnly = false)
	public void save(Recharge recharge) {
		super.save(recharge);
	}
	
	@Transactional(readOnly = false)
	public void delete(Recharge recharge) {
		super.delete(recharge);
	}

	@Transactional(readOnly = false)
	public AjaxJson wxRecharge(Member member, String totalMoney, String rechargePic, String bankId) {
		AjaxJson aj = new AjaxJson();
		if (rechargePic.equals("undefined")||rechargePic.equals("null")||rechargePic.equals("")){
			aj.setSuccess(false);
			aj.setMsg("图片信息有误");
			return aj;
		}
		Recharge recharge = new Recharge();
		recharge.setMemberId(member.getId());
		recharge.setRechargeMoney(Double.valueOf(totalMoney));
		recharge.setRechargePic(rechargePic);
		recharge.setRechargeType(bankId);
		recharge.setRechargeStatus("0");
		save(recharge);
		aj.setSuccess(true);
		aj.setMsg("已提交");
		return aj;
	}

	public List<Recharge> findTodayList(Recharge recharge) {
		return mapper.findTodayList(recharge);
	}

	@Transactional(readOnly = false)
	public void confirm(Recharge recharge) {
		recharge.setRechargeStatus("1");
		Member member = memberService.get(recharge.getMemberId());
		//给充值用户修改余额
		Account account = new Account();
		account.setMember(member);
		List<Account> accountList = accountService.findList(account);
		if (accountList.size()>0){
			BigDecimal yue = BigDecimal.valueOf(accountList.get(0).getYue()).add(BigDecimal.valueOf(recharge.getRechargeMoney()));
			accountList.get(0).setYue(yue.doubleValue());
			accountList.get(0).preUpdate();
			accountMapper.update(accountList.get(0));
		}
		save(recharge);

	}
}